/**
 * Created by jiangtao on 16/6/21.
 */
/**
 * Created by jiangtao on 16/6/17.
 */
define(function(require, exports, module) {
    var addActivity = {
        init: function(){
            var use  = ["render","setVm","VueInit","uploadImage"];
            use.forEach(function(name){
                for(var key in this) {
                    if(key == name) {
                        this[key]();
                    };
                };
            }.bind(this));
        },
        loading:window.loading(),
        util:require("JS/common/util.js"),
        element: document.getElementById('app'),
        template: require("TEMPLATE/activity/addActivity.tpl"),
        render: function(){
            this.element.innerHTML = this.template;
        },
        Vm:null,
        setVm: function(){
            if(!this.Vm) this.Vm = new Vue({
                el: this.element,
                data: {
                    uploadImage:[]
                }
            });
        },
        VueInit: function(){
            var vm   = this.Vm,
                self = this;
            vm.deleteImage = function($index){
               this.uploadImage.splice($index,1);
            };
            vm.formSubmit = function(){
                self.loading.show('加载中');
                setTimeout(function(){
                    self.loading.close();
                },3000)

            };
        },
        getFormData:function(){
            var formData = [];
            $('.add_activity_page').find('input,textarea').each(function(){
                var name = $(this).attr('name'),
                    value = $(this).val();
                if(name) {
                    formData.push({
                        name:name,
                        value:value
                    });
                };
            });
            return formData;
        },
        uploadImage:function(){
            var vm = this.Vm;
            var self = this;
            this.util.uploadImage({
                el:"#file_upload",
                url:"/upload",
                size:5,
                before:function(){
                   self.loading.show('图片上传中')
                },
                success:function(data){
                    if(data.code == 200) {
                        vm.uploadImage.push({url:data.data.url});
                        self.loading.close();
                    };
                },
                error:function(){
                    self.loading.close();
                }
            });
        }
    }

   addActivity.init();
});